package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SessionTest
 */
@WebServlet("/SessionTest")
public class SessionTest extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SessionTest() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String ueberschrift;
		Integer zaehler = (Integer) session.getAttribute("zaehler");
		if (zaehler == null) {
			zaehler = new Integer(0);
			ueberschrift = "Herzlich willkommen Neukunde";
		} else {
			zaehler = new Integer(zaehler.intValue() + 1);
			ueberschrift = "Herzlich willkommen Stammkunde";
		}
		session.setAttribute("zaehler", zaehler);

		// Ausgabe
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<h1>Session</h1>");
		out.println("<h2>"+ueberschrift+"</h1>");
		out.println("Session-ID: " + session.getId() + "<br />");
		out.println("Creation-Time: " + new Date(session.getCreationTime())
				+ "<br />");
		out.println("LastAccessedTime: "
				+ new Date(session.getLastAccessedTime()) + "<br />");
		out.println("Zaehler: " + zaehler + "<br />");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
